/*
 *    Copyright 2006-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 支持生成lombok bean的插件
 * @author liangxiaoman added
 * @since 2021-8-28 17:38:29
 */
public class LombokPlugin extends PluginAdapter {

	public LombokPlugin() {
		super();
	}

	@Override
	public boolean modelGetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		// 不生成getter
        return false;
    }

	@Override
	public boolean modelSetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		// 不生成setter
		return false;
    }

	@Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {

		// 添加entity的import语句
		topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.Data"));
		topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.experimental.Accessors"));

		// 添加entity的注解
		topLevelClass.addAnnotation("@Data");
		topLevelClass.addAnnotation("@Accessors(chain = false)");

		// 添加entity的注释
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine("* Created By Mybatis Generator For Lombok on " + date2Str(new Date()));
		topLevelClass.addJavaDocLine("*/");

		return true;
    }

	private String date2Str(Date date) {
		ThreadLocal<SimpleDateFormat> dateFormatLocal = new ThreadLocal<SimpleDateFormat>() {
			protected SimpleDateFormat initialValue() {
				return new SimpleDateFormat("yyyy-MM-dd");
			}
		};
		return dateFormatLocal.get().format(date);
	}

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}
}
